<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 评论设置组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Options_Discussion extends Widget_Abstract_Options implements Widget_Interface_Do
{
    /**
     * 执行函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
		$this->user->pass('admin');
    }
    /**
     * 输出表单结构
     *
     * @access public
     * @return Form
     */
    public function form()
    {
        /** 构建表格 */
        $form = new Form($this->security->actionUrl('options-discussion', false));

        /** 评论日期格式 */
        $commentDateFormat = new Form_Text('commentDateFormat', NULL, $this->options->commentDateFormat,
        _t('评论日期格式'), _t('这是一个默认的格式,当你在模板中调用显示评论日期方法时, 如果没有指定日期格式, 将按照此格式输出.') . '<br />'
            . _t('具体写法请参考 <a href="http://www.php.net/manual/zh/function.date.php">PHP 日期格式写法</a>.'));
        $form->addInput($commentDateFormat);

        /** 评论列表数目 */
        $commentsListSize = new Form_Text('commentsListSize', NULL, $this->options->commentsListSize,
        _t('评论列表数目'), _t('此数目用于指定显示在侧边栏中的评论列表数目.'));
        $form->addInput($commentsListSize->addRule('isInteger', _t('请填入一个数字')));

        $commentsShowOptions = array(
            'commentsPageBreak'     =>  _t('启用分页, 并且每页显示 %s 篇评论, 在列出时将 %s 作为默认显示',
            '</label><input type="text" value="' . $this->options->commentsPageSize
            . '" style="width:40px;" id="commentsShow-commentsPageSize" name="commentsPageSize" /><label for="commentsShow-commentsPageSize">',
            '</label><select id="commentsShow-commentsPageDisplay" name="commentsPageDisplay">
            <option value="first"' . ('first' == $this->options->commentsPageDisplay ? ' selected="true"' : '') . '>' . _t('第一页') . '</option>
            <option value="last"' . ('last' == $this->options->commentsPageDisplay ? ' selected="true"' : '') . '>' . _t('最后一页') . '</option></select>'
            . '<label for="commentsShow-commentsPageDisplay">'),
            'commentsThreaded'      =>  _t('启用评论回复, 以 %s 层作为每个评论最多的回复层数',
            '</label><input name="commentsMaxNestingLevels" type="text" style="width:40px;" value="' . $this->options->commentsMaxNestingLevels . '" id="commentsShow-commentsMaxNestingLevels" />
            <label for="commentsShow-commentsMaxNestingLevels">') . '</label></span><span class="multiline">'
            . _t('将 %s 的评论显示在前面', '<select id="commentsShow-commentsOrder" name="commentsOrder">
            <option value="DESC"' . ('DESC' == $this->options->commentsOrder ? ' selected="true"' : '') . '>' . _t('较新的') . '</option>
            <option value="ASC"' . ('ASC' == $this->options->commentsOrder ? ' selected="true"' : '') . '>' . _t('较旧的') . '</option></select><label for="commentsShow-commentsOrder">')
        );

        $commentsShowOptionsValue = array();

        if ($this->options->commentsPageBreak) {
            $commentsShowOptionsValue[] = 'commentsPageBreak';
        }

        if ($this->options->commentsThreaded) {
            $commentsShowOptionsValue[] = 'commentsThreaded';
        }

        $commentsShow = new Form_Checkbox('commentsShow', $commentsShowOptions,
        $commentsShowOptionsValue, _t('评论显示'));
        $form->addInput($commentsShow->multiMode());

        /** 评论提交 */
        $commentsPostOptions = array(
            'commentsCheckReferer'          =>  _t('检查评论来源页 URL 是否与文章链接一致'),
            'commentsAutoClose'             =>  _t('在文章发布 %s 天以后自动关闭评论',
            '</label><input name="commentsPostTimeout" type="text" style="width:40px;" value="' . intval($this->options->commentsPostTimeout / (24 * 3600)) . '" id="commentsPost-commentsPostTimeout" />
            <label for="commentsPost-commentsPostTimeout">'),
            'commentsPostIntervalEnable'    =>  _t('用户发布评论的时间间隔限制为 %s 分钟',
            '</label><input name="commentsPostInterval" type="text" style="width:40px;" value="' . round($this->options->commentsPostInterval / (60), 1) . '" id="commentsPost-commentsPostInterval" />
            <label for="commentsPost-commentsPostInterval">')
        );

        if ($this->options->commentsCheckReferer) {
            $commentsPostOptionsValue[] = 'commentsCheckReferer';
        }

        if ($this->options->commentsAutoClose) {
            $commentsPostOptionsValue[] = 'commentsAutoClose';
        }

        if ($this->options->commentsPostIntervalEnable) {
            $commentsPostOptionsValue[] = 'commentsPostIntervalEnable';
        }

        $commentsPost = new Form_Checkbox('commentsPost', $commentsPostOptions,
        $commentsPostOptionsValue, _t('评论提交'));
        $form->addInput($commentsPost->multiMode());

        /** 允许使用的HTML标签和属性 */
        $commentsHTMLTagAllowed = new Form_Textarea('commentsHTMLTagAllowed', NULL,
        $this->options->commentsHTMLTagAllowed,
        _t('允许使用的HTML标签和属性'), _t('默认的用户评论不允许填写任何的HTML标签, 你可以在这里填写允许使用的HTML标签.') . '<br />'
            . _t('比如: %s', '<code>&lt;a href=&quot;&quot;&gt; &lt;img src=&quot;&quot;&gt; &lt;blockquote&gt;</code>'));
        $form->addInput($commentsHTMLTagAllowed);

        /** 提交按钮 */
        $submit = new Form_Submit('submit', NULL, _t('保存设置'));
        $form->set($submit);

		/** 插件接口 */
		//HOOK PLUG_Options_Discussion_Form($form, $this);
		
        return $form;
    }

    /**
     * 执行更新动作
     *
     * @access public
     * @return void
     */
    public function updateDiscussionSettings()
    {
		$form = $this->form();
		
		/** 验证格式 */
        if ($message = $form->validate()) {
            $this->result->set(key($message), current($message));
        }

        $settings = $this->request->from(array_keys($form->getInputs()));

        $settings = $this->request->from('commentDateFormat', 'commentsListSize', 'commentsPageSize', 'commentsPageDisplay', 'commentsOrder', 'commentsMaxNestingLevels', 'commentsUniqueIpInterval','commentsPostTimeout', 'commentsPostInterval', 'commentsHTMLTagAllowed', 'commentsIpBlackList');
		
		
        $commentsShow = $this->request->getArray('commentsShow');
        $commentsPost = $this->request->getArray('commentsPost');

        $settings['commentsPageBreak'] = $this->isEnableByCheckbox($commentsShow, 'commentsPageBreak');
        $settings['commentsThreaded'] = $this->isEnableByCheckbox($commentsShow, 'commentsThreaded');
		
		
        $settings['commentsPageSize'] = intval($settings['commentsPageSize']);
        $settings['commentsMaxNestingLevels'] = min(7, max(2, intval($settings['commentsMaxNestingLevels'])));
        $settings['commentsPageDisplay'] = ('first' == $settings['commentsPageDisplay']) ? 'first' : 'last';
        $settings['commentsOrder'] = ('DESC' == $settings['commentsOrder']) ? 'DESC' : 'ASC';


        $settings['commentsCheckReferer'] = $this->isEnableByCheckbox($commentsPost, 'commentsCheckReferer');
        $settings['commentsAutoClose'] = $this->isEnableByCheckbox($commentsPost, 'commentsAutoClose');
        $settings['commentsPostIntervalEnable'] = $this->isEnableByCheckbox($commentsPost, 'commentsPostIntervalEnable');

        $settings['commentsPostTimeout'] = intval($settings['commentsPostTimeout']) * 24 * 3600;
        $settings['commentsPostInterval'] = round($settings['commentsPostInterval'], 1) * 60;

		/** 插件接口 */
		//HOOK $settings | PLUG_Options_Discussion_Update($settings, $this);
		
        foreach ($settings as $name => $value) {
            $this->update($name, $value);
        }

		$this->select()
			->fetchAll(array($this, 'push'));
		
		/** 插件接口 */
		//HOOK PLUG_Options_Discussion_Update_Success($this);
		
        $this->result->set(0, _t("设置已经保存"));
    }

    /**
     * 绑定动作
     *
     * @access public
     * @return void
     */
    public function action()
    {
        $this->security->protect();
        $this->on($this->request->isPost())->updateDiscussionSettings();
        $this->result->set(404, _t('请求的资源不存在'));
    }
}
